package com.lw.spring.javademo.jsp;


import com.lw.spring.javademo.model.User;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user-map")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建用户对象
        User user1 = new User("狗蛋", 25, "北京");
        User user2 = new User("李四", 30, "上海");
        User user3 = new User("王五", 28, "广州");

        // 将用户对象存储到 Map 中
        Map<String, User> userMap = new HashMap<>();
        userMap.put("user1", user1);
        userMap.put("user2", user2);
        userMap.put("user3", user3);

        // 将 Map 数据放入请求属性
        request.setAttribute("userMap", userMap);//////

        // 转发到 JSP 页面
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/userMap.jsp");
        dispatcher.forward(request, response);
    }
}
